<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #recordReminderPeriod="{ filterForm }">
        <el-form-item label="提醒日期">
          <el-date-picker
            v-model="filterForm.time"
            type="daterange"
            range-separator="-"
            style="width: 100%"
            @change="operationTimeChange"
            format="MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['securityStaffCertificateRecord:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          full-path="/securityStaffCertificateRecord/importData"
          plain
          icon="Plus"
          @success="getList"
          v-hasPermi="['securityStaffCertificateRecord:importData']"
        ></ImportExcel>
      </el-col>
      <el-col :span="1.5">
        <ExportExcel
          type="primary"
          full-path="/securityStaffCertificateRecord/importTemplate"
          v-hasPermi="['securityStaffCertificateRecord:importTemplate']"
          plain
        ></ExportExcel>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      @selectionChange="selectionChange"
      :rowClassName="rowClassName"
    >
      <template #safetyAcceptor="{ row }">
        {{ row.safetyAcceptorName }}
      </template>
      <template #safetyLeader="{ row }">
        {{ row.safetyLeaderName }}
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['securityStaffCertificateRecord:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['securityStaffCertificateRecord:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSolt', 'safetyLeader']"
    >
      <template #fileSolt>
        <el-form-item label="证书照片" label-position="right">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['png', 'jpeg', 'jpg']"
            v-model="files"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/ygzs.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import FileUpload from "@/components/FileUpload/index";
import ImportExcel from "@/components/ImportExcel/index.vue";
import ExportExcel from "@/components/ExportExcel/index.vue";
const operationTimeChange = (data) => {
  if (data && data.length) {
    queryParams.value.recordReminderPeriodStartDate = data[0];
    queryParams.value.recordReminderPeriodEndDate = data[1];
  } else {
    queryParams.value.recordReminderPeriodStartDate = null;
    queryParams.value.recordReminderPeriodEndDate = null;
  }
};
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "recordName",
      "recordCertificateNumber",
      "recordCertificateType",
      "recordLicenceIssuing",
      "recordReminderPeriod",
    ].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();

const plugEdit = (row) => {
  if (row.recordRecordFileId) {
    files.value = [
      {
        name: row.recordRecordFileName,
        id: row.recordRecordFileId,
        url: row.recordRecordFileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const plugAdd = (row) => {
  files.value = [];
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
  handleExport,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugAdd,
});
const selectList = ref([]);
const selectionChange = (rows) => {
  selectList.value = rows;
};

const files = ref([]);
watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.recordRecordFileName = "";
      form.value.recordRecordFileId = "";
      form.value.recordRecordFileUrl = "";
    } else {
      form.value.recordRecordFileName = newData.value[0].name;
      form.value.recordRecordFileId = newData.value[0].id;
      form.value.recordRecordFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);

const rowClassName = ({ row, rowIndex }) => {
  const nowTime = Date.now();
  const endTime = new Date(row.recordValidEndDate).getTime();
  const txTime = new Date(row.recordReminderPeriod).getTime();
  if (endTime && nowTime >= endTime) {
    return "deepred";
  } else if (txTime && nowTime >= txTime && nowTime <= endTime) {
    return "yellow";
  } else {
    return "";
  }
};
</script>

<style lang="scss" scoped></style>
